package com.example.forms;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.sale24h.MainActivity;
import com.example.sale24h.R;
import com.example.serverconnect.ITaskListener;
import com.sale24h.data.AppData;
import com.sale24h.data.clsLoaiHang;
import com.sale24h.data.clsMatHang;
import com.sale24h.data.clsNhomHang;
import com.sale24h.data.eFRMs;
import com.sale24h.data.adapter.MathangAdapter;
import com.sale24h.data.adapter.SpinnerItemAdapter;
import com.sale24h.tasks.GetAllMatHang;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Spinner;

public class frmChonMon extends android.support.v4.app.Fragment
{
	private Context context;
	private Spinner spnLoaiHang;
	private Spinner spnNhomHang;
	private ListView lstvMathangs;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View rootView=inflater.inflate(R.layout.frmchonmon_layout, container,false);
		this.context=rootView.getContext();
		
		spnLoaiHang=(Spinner)rootView.findViewById(R.id.spnLoaiHang);
		spnNhomHang=(Spinner)rootView.findViewById(R.id.spnNhomHang);
		lstvMathangs=(ListView)rootView.findViewById(R.id.lstvMatHangs);
		
		spnLoaiHang.setOnItemSelectedListener(new SpinerItemSelectedClick());
		spnNhomHang.setOnItemSelectedListener(new SpinerItemSelectedClick());
		lstvMathangs.setOnItemClickListener(new ListViewViewItemClick());		
		
		LoadData();
		return rootView;
	}
	private class ListViewViewItemClick implements OnItemClickListener
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			// list mat hang item click
			MainActivity.getInstance().gotoView(eFRMs.FRM_PGOIMON);	
			//frmPhieuGoiMon.getInstance().AddMonAn((clsMatHang)view.getTag());
			
		}
		
	}
	
	private class SpinerItemSelectedClick implements AdapterView.OnItemSelectedListener
	{

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position,
				long id)
		{
			if(parent.getId()==R.id.spnLoaiHang)
			{
				//select loai hang
				// apply data to view
				clsLoaiHang current=AppData.getInstance().getLoaiHang(position);
				
				AppData.getInstance().setCurrentLoaiHang(current);
				
				SpinnerItemAdapter<clsNhomHang> adpnh=new SpinnerItemAdapter<clsNhomHang>(context, 
												current.getLstNhomHangs());
				
				adpnh.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				
				spnNhomHang.setAdapter(adpnh);				
			}
			else if(parent.getId()==R.id.spnNhomHang)
			{
				//select nhom hang
				clsLoaiHang currentLoaiHang=AppData.getInstance().getCurrentLoaiHang();
				clsNhomHang currentNhomHang=currentLoaiHang.getNhomHang(position);
				
				lstvMathangs.setAdapter(
						new MathangAdapter(context, android.R.layout.simple_list_item_1,
						currentNhomHang.getLstMatHangs()));
			}
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0)
		{
			// TODO Auto-generated method stub
			
		}
		
	}


	private void LoadData()
	{		
		new GetAllMatHang(new ITaskListener()
		{
			
			@Override
			public void PreRun()
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void Completed(JSONObject result)
			{
				try
				{
					AppData.getInstance().Apply_LoaiHang_NhomHang_Mathangs(result);
					
					// apply data to view
					SpinnerItemAdapter<clsLoaiHang> adplh=new SpinnerItemAdapter<clsLoaiHang>(context, 
							AppData.getInstance().getLstLoaiHangs());
					
					adplh.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					
					spnLoaiHang.setAdapter(adplh);
				}
				catch (JSONException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}, this.context).execute();
		
	}

}
